package com.test;

import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

/**
 * HelloClient
 *
 * @author longhuashen
 * @since 2023/11/10
 */
public class HelloClient {

    public static final String SERVER_IP = "localhost";
    public static final int SERVER_PORT = 8090;
    public static final int TIMEOUT = 30000;

    public void startClient(String userName){
        TTransport transport = null;
        try {
            transport = new TSocket(SERVER_IP,SERVER_PORT,TIMEOUT);
            //协议要和服务端一致
            TProtocol protocol  = new TBinaryProtocol(transport);

            HelloWorldService.Client client = new HelloWorldService.Client(protocol);
            transport.open();

            String result = client.sayHello(userName);
            System.out.println("Thrift client result is:"+result);

        } catch (Exception e) {
            System.out.println("client is error!");
        }finally{
            if(null != transport){
                transport.close();
            }
        }
    }
    public static void main(String[] args) {
        HelloClient client = new HelloClient();
        client.startClient("Z Yong");
    }
}
